# -*- coding: UTF-8 -*-
import json

from pymongo import MongoClient
from datetime import datetime, timezone
import pandas as pd

desired_width = 320
pd.set_option('display.width', desired_width)
pd.set_option('display.max_columns', 20)


def import_block_feature_from_csv(csv_filepath, db, collection, window):
    data = pd.read_csv(csv_filepath)
    data = data[::window]
    # data = data[:10]
    data['_id'] = data.apply(
        lambda row: int(datetime.strptime(row['time'], '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc).timestamp()),
        axis=1)
    # del data['time']
    data = data[data['time'] < '2018-12-06 02:00:00']
    records = json.loads(data.T.to_json()).values()
    db[collection].insert_many(records)


def import_price_from_csv(price_csv_filepath, db, collection):
    data = pd.read_csv(price_csv_filepath)
    # data = data[::60]
    # data = data[:10]
    data['_id'] = data.apply(
        lambda row: 1000 * int(datetime.strptime(row['time'], '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc).timestamp()),
        axis=1)
    # del data['time']
    records = json.loads(data.T.to_json()).values()
    db[collection].insert_many(records)


if __name__ == '__main__':
    # 从文件中批量导入block特征
    csv_filepath = './data/features/block_0601_1207-feature-60.csv'
    # csv_filepath = './data/features/block_0601_1207-feature-15.csv'
    # csv_filepath = './data/features/block_0601_1207-feature-30.csv'
    features_db = MongoClient('mongodb://39.104.227.148:27019/')['features']
    collection = 'block_60'
    window = 60
    import_block_feature_from_csv(csv_filepath, features_db, collection, window)

    # price_csv_filepath = '../data/okex/OKEX_BTC_20180601_20181206.csv'
    # db = MongoClient('mongodb://39.104.227.148:27018/')['exchange']
    # collection = 'okex_btc_usdt_test'
    # import_price_from_csv(price_csv_filepath, db, collection)
    pass
